package com.wfuhui.modules.oms.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wfuhui.common.utils.Query;
import com.wfuhui.common.utils.R;
import com.wfuhui.modules.oms.entity.GoodsEntity;
import com.wfuhui.modules.oms.service.GoodsService;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;


/**
 * 商品
 *
 */
@RestController
@RequestMapping("/oms/goods")
public class GoodsController {
	@Autowired
	private GoodsService goodsService;
	
	@RequestMapping("/exportExcel")
	@RequiresPermissions("goods:list")
	public void exportInExcel(@RequestParam Map<String, Object> params, HttpServletResponse resp) throws IOException {
		List<GoodsEntity> goodsList = goodsService.queryList(params);
		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("商品列表", "商品列表"), GoodsEntity.class, goodsList);
		resp.setHeader("Content-disposition", "goods.xls");
		resp.setContentType("application/vnd.ms-excel");
		resp.setHeader("Content-disposition", "attachment;filename=goods.xls");
		resp.setHeader("Pragma", "No-cache");
		workbook.write(resp.getOutputStream());
	}
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("goods:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<GoodsEntity> goodsList = goodsService.queryList(query);
		int total = goodsService.queryTotal(query);
		
		return R.ok().put("rows", goodsList).put("total", total);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{goodsId}")
	@RequiresPermissions("goods:info")
	public R info(@PathVariable("goodsId") Integer goodsId){
		GoodsEntity goods = goodsService.queryObject(goodsId);
		
		return R.ok().put("goods", goods);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("goods:save")
	public R save(@RequestBody GoodsEntity goods){
		goods.setCreateTime(new Date());
		goodsService.save(goods);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("goods:update")
	public R update(@RequestBody GoodsEntity goods){
		goodsService.update(goods);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("goods:delete")
	public R delete(@RequestBody Integer[] goodsIds){
		goodsService.deleteBatch(goodsIds);
		
		return R.ok();
	}
	
	@RequestMapping("/getAll")
	@RequiresPermissions("goods:list")
	public R getAll() {
		List<GoodsEntity> goodsList = goodsService.queryList(null);
		return R.ok().put("goodsList", goodsList);
	}
	
}
